package servlet;

import pojo.Rent;
import service.RentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        RentService es = new RentService();

        String userName = request.getParameter("userName");
        String carNum = request.getParameter("carNum");
        int status = Integer.parseInt(request.getParameter("status"));
        String maturityDate = request.getParameter("maturityDate");
        int result = es.addRent(new Rent(0,userName,carNum,status,maturityDate));
        if (result>0){
            out.print("<script>alert('添加成功');location.href=`ListServlet`;<script>");
        }else {
            out.print("<script>alert('添加失败')；history.back();<script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
